<include file="header"/>
<body>
<div class="wraper">
    <div class="load">
        <img class="load-bee" src="/assets/base/repair/images/load-bee.png"/>
    </div>
    <div class="top">
        <p>
            <a href="{BEESCRM::U('base/repair/orderList',array('repairId'=>$repairId))}"
               class="iconfont icon-zuo fl-left"></a>
            订单详情
            <a href="{BEESCRM::U('base/repair/orderList',array('repairId'=>$repairId))}"
               class="iconfont icon-shouye fl-right"></a>
        </p>
    </div>
    <p class="title">报修序号</p>
    <p class="list" style="margin-bottom: 10px;">{BEESCRM:$data.order_sort}</p>
    <!--已下单-->
    <div class="dialog clearfix">
        <span class="left-big"></span>
        <span class="left-middle"></span>
        <p>
            <span class="circle gray"></span>已下单<span class="time fl-right">{BEESCRM:$data.create_time|date='Y-m-d H:i',###}</span>
        </p>
        <p><span>报修人：</span> {BEESCRM:$data['real_name']|mb_substr=0,1,'utf8'}某某</p>
        <p><span>联系电话：</span> <a href="tel://{BEESCRM:$data.mobile}">{BEESCRM:$data.mobile}</a></p>
        <p><span>报修区域：</span> {BEESCRM:$data.community_name}</p>
        <p><span>报修地址：</span>
            {BEESCRM:$data.building_name}{BEESCRM:$data.room_name}{BEESCRM:$addressInfo['address_detail']}</p>
        <p><span>报修内容：</span> {BEESCRM:$data.content}</p>
        <P><span>报修项目：</span>{BEESCRM:$data.project_name}　　{BEESCRM:$data.project_child_name}</P>
        <P><span>报修金额：</span>{BEESCRM:$data.cost}元</P>
        <if condition="$data['RepairOrderImage']">
            <volist name="data['RepairOrderImage']" id="vo">
                <if condition="$vo['type'] neq 1"><img class="picture fl-left" src="{BEESCRM:$vo.url}"/></if>
            </volist>
        </if>
        <if condition="($data['status'] eq 1 or $data['status'] eq 2) and ($parent_position eq 1) and ($data['cut_off'] eq 0)">
            <a href="#" class="confirm fl-right" id="confirm">确认订单</a>
        </if>
        <if condition="($data['status'] elt 2)and ($parent_position eq 0) and($data['abolish_type'] eq 0)">
            <a href="#" class="confirm fl-right" id="abolish">取消订单</a>
        </if>
    </div>

    <if condition="($data['status'] eq 1)and $data['abolish_type'] neq 2 and ($data['cut_off'] eq 1)  and ($parent_position eq 1) and ($userId eq $data['repairman_id'])" >
    <div class="dialog clearfix">
        <span class="right-big"></span>
        <span class="right-middle"></span>
        <p>
            <span class="circle green"></span>
            已取消
        </p>
        <p><span>取消人:</span> {BEESCRM:$data.real_name}</p>
        <p><span>取消说明:</span> {BEESCRM:$data.remark}</p>
        <p><span>取消类型:</span>
            <if condition="$data['abolish_type'] eq 1 ">报修人取消
                <elseif condition="$data['abolish_type'] eq 2"/>
                维修工取消
                <elseif condition="$data['abolish_type'] eq 3"/>
                管理员取消
            </if>
        </p>
    </div>
    </if>
    <!--已确认-->

    <if condition="$data['send_time'] and $data['repairman_id'] and $data['confirm_time'] ">
        <div class="dialog clearfix">
            <span class="right-big"></span>
            <span class="right-middle"></span>
            <p>
                <span class="circle orange"></span>
                已确认
                <span class="time fl-right">{BEESCRM:$data.send_time|date='Y-m-d H:i',###}</span>
            </p>
            <p><span>受理人：</span>
                <if condition="$data['confirm_real_name'] neq null">
                    {BEESCRM:$data['confirm_real_name']|mb_substr=0,1,'utf8'}
                    <else/>
                    {BEESCRM:$data['repairman_real_name']|mb_substr=0,1,'utf8'}
                </if>
                师傅
            </p>
            <p><span>受理人电话：</span>
                <if condition="$data['repairman_mobile'] eq null">暂无
                    <else/>
                    <a href="tel://{BEESCRM:$data.repairman_mobile}">{BEESCRM:$data['repairman_mobile']}</a></if>
            </p>
            <p><span>承修负责人：</span>
                {BEESCRM:$data['repairman_real_name']|mb_substr=0,1,'utf8'}师傅　工号：{BEESCRM:$data['repairman_number']}</p>
            <p><span>处理说明：</span> {BEESCRM:$data.send_remark}</p>
            <if condition="($data['status'] eq 4)and ($parent_position eq 0) and($data['abolish_type'] eq 0) ">
                <a href="#" class="confirm fl-right" id="abolish">取消订单</a>
            </if>
        </div>
    </if>


    <if condition="$data['cut_off'] eq 1 and ($data['status'] eq 4) and $userId neq $data['repairman_id'] and $data['abolish_type'] eq 2">
        <div class="dialog clearfix">
            <span class="right-big"></span>
            <span class="right-middle"></span>
            <p>
                <span class="circle green"></span>
                已取消
            </p>
            <p><span>取消说明:</span> {BEESCRM:$data.remark}</p>
            <p><span>返修分类:</span>一级报修类目：{BEESCRM:$data.new_project_name}
                <notempty name="data.new_project_child_name">&nbsp;&nbsp;&nbsp;二级报修类目:{BEESCRM:$data.new_project_child_name}</notempty>
            </p>
            <a href="{BEESCRM::U('base/repair/createOrder',array('repairId'=>$repairId,'order_id'=>$data['order_id'],'is_back'=>1))}"
               class="confirm">重新下单</a>
        </div>
    </if>

    <if condition="$data['cut_off'] eq 1 and $data['abolish_type'] neq 2 and ($userId neq $data['repairman_id'])">
        <div class="dialog clearfix">
            <span class="right-big"></span>
            <span class="right-middle"></span>
            <p>
                <span class="circle green"></span>
                已取消
            </p>
            <p><span>取消人:</span> {BEESCRM:$data.real_name}</p>
            <p><span>取消说明:</span> {BEESCRM:$data.remark}</p>
            <p><span>取消类型:</span>
                <if condition="$data['abolish_type'] eq 1 ">报修人取消
                    <elseif condition="$data['abolish_type'] eq 2"/>
                    维修工取消
                    <elseif condition="$data['abolish_type'] eq 3"/>
                    管理员取消
                </if>
            </p>
        </div>
    </if>
    <include file="repairclient"/>
    <include file="repairman"/>
    <include file="pay"/>
    <include file="access"/>
</div>

<!--无法维修弹窗-->
<div class="layer-wraper" id="do_abolish">
    <div class="main">
        <h4><p class="field">取消原因</p></h4>
        <textarea placeholder="请简要描述取消原因" id="abolish_content"></textarea>
        <div class="pop-btn">
            <a href="javascript:void(0);" class="pbtn cancel">取消</a>
            <a href="javascript:void(0);" class="pbtn blue-btn">确认提交</a>
        </div>
    </div>
</div>
<include file="commonJS"/>
<script type="text/javascript" src="/assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.fileupload.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.iframe-transport.js"></script>
<script src="/assets/base/repair/js/fastclick.min.js"></script>
<script>
    $(function () {
        //FastClick.attach(document.body);
        $('#abolish').click(function () {
            $('#abolish_content').val('');
            $('#do_abolish').show();
        });

        $('.cancel').click(function () {
            $('#do_abolish').hide();
        });

        $('#abolish').click(function () {
            $('#abolish_content').val('');
            $('#do_abolish').show();
        });

        $('#cancel2').click(function () {
            $('#repairman_layer').show();
        });

        $('.cancel').click(function () {
            $('#repairman_layer').hide();
        });

        $('#firstCategory').change(function () {
            var categoryId = $(this).val();
            if (categoryId == 0) {
                $('#secondCategory').empty();
                return false;
            }

            $.ajax({
                url: "{BEESCRM::U('base/repair/getChildProject',array('repairId'=>$repairId))}",
                'type': 'post',
                data: {'id': categoryId},
                dataType: "json",
                success: function (res) {
                    var result = res.result;
                    $('#secondCategory').empty();
                    if (result.length > 0) {
                        var options = '<select class="select" id="secondCat"><option value="0" selected="selected">请选择二级分类</option>';
                        $.each(result, function (index, node) {
                            var option = '<option value="' + node.project_id + '">' + node.project_name + '</option>';
                            options += option;
                        });
                        options += '</select><span class="triangle"></span>';
                        $('#secondCategory').html(options);
                    }

                }
            })
        })

        //返修提交
        $('.blue-btn').click(function () {
            var data = {};
            var parent_position = {BEESCRM:$parent_position};
            data.parent_position = parent_position;//后台判断是否是维修工取消订单
            if (parent_position == 1) {
                data.remark = $('#content').val();
                data.orderId = "{BEESCRM:$data.order_id}";
                data.firstCategory = $('#firstCategory').val();
                data.secondCat = $('#secondCat').val();
                if (!data.remark) {
                    $.alert('error', '请填写无法维修原因');
                    return false;
                }
                if (parseInt(data.firstCategory) == 0) {
                    $.alert('error', '请选择返修一级类目');
                    return false;
                }
            }
            if (parent_position == 0) {
                data.remark = $('#abolish_content').val();
                data.orderId = "{BEESCRM:$data.order_id}";
                if (!data.remark) {
                    $.alert('error', '请填写取消维修原因');
                    return false;
                }
            }
            $.ajax({
                url: "{BEESCRM::U('base/repair/back',array('repairId'=>$repairId))}",
                type: "post",
                data: data/*{'remark':remark,'firstCategory':firstCategory,'secondCat':secondCat,'orderId':orderId}*/,
                dataType: "json",
                success: function (res) {
                    if (res.status == 'F') {
                        $.alert('error', res.message);
                        $('.repairman_layer').hide();
                    } else {
                        $.alert('success', res.message);
                        $('.repairman_layer').hide();
                        //window.location.href = "/base/repair/createOrder";
                        location.reload();
                    }
                }
            })
        })

        $("#fileupload_input").fileupload({
            url: "/base/common/upload",//文件上传地址，当然也可以直接写在input的data-url属性内
            done: function (e, result) {
                //done方法就是上传完毕的回调函数，其他回调函数可以自行查看api
                //注意result要和jquery的ajax的data参数区分，这个对象包含了整个请求信息
                //返回的数据在result.result中，假设我们服务器返回了一个json对象
                var data = $.parseJSON(result.result);
                if (data.status == 'S') {
                    $('#picture').attr('src', data.url);
                } else {
                    $.alert('error', data.message);
                }
            }
        });
        //星级评价
        $('body').on('click', '.star', function () {
            $(this).removeClass('gray-star');
            $(this).prevAll('.star').removeClass('gray-star');
            $(this).nextAll('.star').addClass('gray-star');
            var num = $(this).parent().find('.gray-star').length;
            if (num >= 3) {
                $(this).siblings('.level').html('差评');
            } else if (num < 2) {
                $(this).siblings('.level').html('好评');
            } else {
                $(this).siblings('.level').html('中评');
            }
        });

        //确认订单
        $(document).on('click', '#confirm', function () {
            $.ajax({
                type: 'post',
                url: "{BEESCRM::U('base/repair/orderConfirm',array('repairId'=>$repairId))}",
                dataType: 'json',
                data: {
                    'order_id': "{BEESCRM:$data.order_id}",
                    'status': 4,
                },
                success: function (data) {
                    if (data.status == 'S') {
                        $.alert('success', data.message);
                        setTimeout("window.location.reload()", 3000);
                    } else {
                        $.alert('error', data.message);
                    }
                },
                error: function () {
                    $.alert('error', '通讯失败，请稍后再试');
                }
            });
        });

        //确认完工
        $(document).on('click', '#finish_submit', function () {
            var data = {};
            data.order_id = "{BEESCRM:$data.order_id}";
            data.finish_remark = $('#finish_remark').val();
            if ($('#order_amount').val() != '') {
                data.order_amount = $('#order_amount').val();
            }
            data.url = $('#picture')[0].src;

            $.ajax({
                type: 'post',
                url: "{BEESCRM::U('base/repair/orderfinish',array('repairId'=>$repairId))}",
                dataType: 'json',
                data: data,
                success: function (data) {
                    if (data.status == 'S') {
                        $.alert('success', data.message);
                        setTimeout("window.location.reload()", 3000);
                    } else {
                        $.alert('error', data.message);
                    }
                },
                error: function () {
                    $.alert('error', '通讯失败，请稍后再试');
                }
            });
        });

        //提交评价
        $(document).on('click', '#submit', function () {
            if ($('.quality:not(.gray-star)').length <= 0 || $('.service:not(.gray-star)').length <= 0 || $('.image:not(.gray-star)').length <= 0) {
                $.alert('error', '请完成所有评价，再提交~');
                return false;
            }
            $.ajax({
                type: 'post',
                url: "{BEESCRM::U('base/repair/ordercomment',array('repairId'=>$repairId))}",
                dataType: 'json',
                data: {
                    'order_id': "{BEESCRM:$data.order_id}",
                    'comment': $('#comment').val(),
                    'quality_star': $('.quality:not(.gray-star)').length,
                    'service_star': $('.service:not(.gray-star)').length,
                    'image_star': $('.image:not(.gray-star)').length
                },

                success: function (data) {
                    if (data.status == 'S') {
                        $.alert('success', data.message);
                        setTimeout("window.location.reload()", 3000);
                    } else {
                        $.alert('error', data.message);
                    }
                },
                error: function () {
                    $.alert('error', '通讯失败，请稍后再试');
                }
            });
        });


        /*支付*/
        $('#doPay').on('click', function () {
            $.ajax({
                url: "{BEESCRM::U('Base/Repair/doPay',array('repairId'=>$repairId))}",
                type: "post",
                data: {'order_id': "{BEESCRM:$data.order_id}"},
                dataType: 'json',
                success: function (data) {
                    if (data.status == 'F') {
                        $.alert('error', data.message);
                        return;
                    } else {
                        /* alert(data.data.order_id);
                         return ; */
                        /*  if("598" == "{BEESCRM::session('fans_id')}"){
                         paySuccess(data.data.order_id);
                         }else{
                         pay(data.wxpay);
                         }  */
                        var id = data.data.order_id;
                        var wxpay = $.parseJSON(data.wxpay);
                        pay(wxpay, id);
                    }
                }
            })
        })
    });

    //微支付
    function pay(wxpay, id) {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        } else {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                wxpay,
                function (res) {
                    WeixinJSBridge.log(res.err_msg);

                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        location.reload();
                    } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                        $.alert('error', '付款已取消');
                        return false;
                    } else {
//							$.alert('error','调用微支付失败');
                        $.alert('error', res.err_msg);
                        return false;
                    }
                }
            );
        }
    }

    //下面这个函数用来转换数组到json格式
    function arrayToJson(o) {
        var r = [];
        if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
        if (typeof o == "object") {
            if (!o.sort) {
                for (var i in o)
                    r.push(i + ":" + arrayToJson(o[i]));
                if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                    r.push("toString:" + o.toString.toString());
                }
                r = "{" + r.join() + "}";
            } else {
                for (var i = 0; i < o.length; i++) {
                    r.push(arrayToJson(o[i]));
                }
                r = "[" + r.join() + "]";
            }
            return r;
        }
        return o.toString();
    }
    //这个是调用微信图片浏览器的函数
    function imagePreview(curSrc, srcList) {
        //这个检测是否参数为空
        if (!curSrc || !srcList || srcList.length == 0) {
            return;
        }
        //这个使用了微信浏览器提供的JsAPI 调用微信图片浏览器
        WeixinJSBridge.invoke('imagePreview', {
            'current': curSrc,
            'urls': srcList
        });
    };

    (function ($) {
        //下面是获取当前页面所有的img的src 转成数组 并且 转换成json格式
        var aa = [];
        var i = 0;
        var src = [];
        var json = null;
        aa = $('img');
        for (i = 0; i < aa.length; i++) {
            src[i] = aa[i].src;    //把所有的src存到数组里
        }
        var srcList = arrayToJson(src); //转换成json并赋值给srcList
        //下面是点击图片的时候获取当前第几个图片并且启用咱们做的调用微信图片浏览器的函数
        $('img').click(function () {
            var index = $('img').index(this);
            imagePreview(src[index], src);
        });

    })(jQuery);
</script>
</body>

</html>
